
using QQZiFramework;
using QQZiFramework.MQ;
using RabbitMQ.Client;

namespace TestProject1
{
    [TestClass]
    public class TestRabbitMQ
    {
        DBContext Context;
        public TestRabbitMQ()
        {
            var _OEMProgram = new OEMProgram("JPAAS");
            _OEMProgram.Run(null);

            Context = _OEMProgram.CreateDBContext();
            //Context.GetMQDriver().BindReceived("TestTopic.Exchange.queue1", (bytes =>
            //{
            //    Console.WriteLine("queue1 consume");
            //    return true;
            //}));

            // Context.GetMQDriver().BindReceived("TestTopic.Exchange.queue2", (bytes =>
            // {
            //     Console.WriteLine("queue2 consume");
            //     return true;
            // }));
            //Context.GetMQDriver().BindReceived("TestTopic.NoExchangeQueue", (bytes => {
            //    return true;
            //}));

        }

        [TestMethod]
        public void TestReceivedAsync()
        {
            IMQDriver mqAccess = Context.GetMQDriver();
            bool isRecive = false;
            mqAccess.BindReceivedAsync("testMQ", async (b) =>
            {
                await Task.Delay(100);
                isRecive = true;
                return true;
            });
            mqAccess.Publish("testMQ", "testMQ");
            DateTime start = DateTime.Now;
            while (true)
            {
                if (isRecive) break;
                Thread.Sleep(200);
                if ((DateTime.Now - start).TotalSeconds > 5)
                {
                    break;
                }
            }
            Assert.IsTrue(isRecive);

        }

        [TestMethod]
        public void TestPublshExchange()
        {
            Context.GetMQDriver().Publish("TestTopic.Exchange", "hello , this is exchange test");
            System.Threading.Thread.Sleep(3000);
        }

        [TestMethod]
        public void TestPublshExchangeQueue1()
        {
            Context.GetMQDriver().Publish("TestTopic.Exchange.queue1", "hello , this is queue1 test");
        }
        [TestMethod]
        public void TestPublshExchangeQueue2()
        {
            Context.GetMQDriver().Publish("TestTopic.Exchange.queue2", "hello , this is queue2 test");
        }

        [TestMethod]
        public void TestPublshQueueNoExchange()
        {
            Context.GetMQDriver().Publish("TestTopic.NoExchangeQueue", "hello , this is queue1 test");
            Context.GetMQDriver().Publish("TestTopic.NoExchangeQueueNotInConfig", "hello , this is queue1 test");
        }

        [TestMethod]
        public void TestGetCount()
        {
            Context.GetMQDriver().Publish("TestTopic.NoExchangeQueue", "hello , this is queue1 test");
            long len= Context.GetMQDriver().GetLength("TestTopic.NoExchangeQueue");
            Assert.IsTrue(len > 0);
            Console.WriteLine("数量：" + len);
        }

        [TestMethod]
        public void TestMulitThreads()
        {
            var conn = RabbitMQConnectShareManage.GetConnection();
            // 设置线程池的最大线程数
            // ThreadPool.SetMaxThreads(50, 50);
            int count = 0;
            //Task[] tasks = new Task[5];
            //for (int i = 0; i < tasks.Length; i++)
            //{
            //    tasks[i] = System.Threading.Tasks.Task.Run(() =>
            //    {
            int cc = 500000;
            for (var j = 0; j < cc; j++)
            {
                Context.GetMQDriver().Publish("TestTopic.NoExchangeQueue", "hello , this is queue test");
                Context.GetMQDriver().Publish("TestTopic.NoExchangeQueue2", "hello , this is queue test2");
                if(j%3==0) System.Threading.Thread.Sleep(1000);

            }
            count += cc;
            System.Diagnostics.Debug.WriteLine($"已发送{count}条消息");

            //    });
            //}

            //Task.WaitAll(tasks);
            Console.WriteLine("finish");
        }
    }
}